package yongxin.service.impl.processreport;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.jeecgframework.core.util.DynamicDBUtil;
import org.jeecgframework.core.util.HttpRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import yongxin.entity.process.ProcessEntity;
import yongxin.entity.processreport.ProcessReportEntity;
import yongxin.entity.tasks.TasksEntity;
import yongxin.kingdee.config.K3CloundConfig;
import yongxin.kingdee.enums.ResultEnum;
import yongxin.kingdee.service.KingdeeService;
import yongxin.kingdee.utils.BaseUtil;
import yongxin.kingdee.vo.ResultVo;
import yongxin.service.processreport.ProcessReportServiceI;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import yongxin.vo.ReportDetail;
import yongxin.vo.YXReport;

import javax.xml.soap.Detail;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("processReportService")
@Transactional
public class ProcessReportServiceImpl extends CommonServiceImpl implements ProcessReportServiceI {

    private static final Logger logger = LoggerFactory.getLogger(ProcessReportServiceImpl.class);


    @Autowired
    private KingdeeService kingdeeService;


    @Override
    public void uploadTaskReportToOver(String reportId,List<ReportDetail> details){
        //DynamicDBUtil.update("kingdee_db","update table set state=?",1);
        try {


            if (details.size() == 0) {
                return;
            }
            String url = "http://192.168.8.7:8082/yx/report";

            String sql = "select a.erpTaskId,a.fbcph,b.updateBy,b.update_Date from t_tasks a ,t_processreport b\n" +
                    "where b.id='" + reportId + "' \n" +
                    "and  (CHARINDEX('-',b.fbcph)>0  and a.fbcph=SUBSTRING(b.fbcph, 0,  CHARINDEX('-',b.fbcph) ) or a.fbcph=b.fbcph) ";

            Map<String, Object> tasksEntity = this.findOneForJdbc(sql);

            YXReport report = new YXReport();
            report.setErpTaskId(tasksEntity.get("erpTaskId") == null ? "" : tasksEntity.get("erpTaskId").toString());
            report.setFbcph(tasksEntity.get("fbcph").toString());
            report.setUpdateBy(tasksEntity.get("updateBy").toString());
            report.setUpdateDate(tasksEntity.get("update_Date").toString());

            report.setDetails(details);



            //登录金蝶系统

            String loginParam = BaseUtil.buildLogin(K3CloundConfig.accId,K3CloundConfig.loginUser,K3CloundConfig.password,K3CloundConfig.lcid);

            ResultVo login = kingdeeService.login(K3CloundConfig.url+K3CloundConfig.login,loginParam);

            if(login.getCode() != ResultEnum.SUCCESS.getCode()){
                logger.error("【登录金蝶系统失败】：{}",login.getMsg());
                return;
            }
            logger.info("【登录金蝶系统】：{}",JSON.toJSONString(login));
            Map<String,Object> map = (Map<String, Object>) login.getData();
            String cookie = map.get("cookie").toString();


            System.out.println(JSON.toJSONString(report));
            String reportData=BaseUtil.buildReport(report);
            System.out.println(reportData);
            // JSONObject result = HttpRequest.sendPost(url, JSON.toJSONString(report));
            ResultVo save =kingdeeService.report(K3CloundConfig.url+K3CloundConfig.save,cookie,reportData);
            if(save.getCode()!=ResultEnum.SUCCESS.getCode()){
                logger.error("【保存出错】：{}",save.getMsg());

                logger.error("汇报结束上传ERP异常:"+reportId);
                logger.error("汇报结束上传ERP异常:"+url);
                logger.error("汇报结束上传ERP异常:"+JSON.toJSONString(report));
                logger.error("汇报结束上传ERP异常==================================================");

                return;
            }



            System.out.println("汇报结束上传ERP: " + JSON.toJSONString(save));
        }
        catch (Exception e){
            e.printStackTrace();
            logger.error("汇报结束上传ERP异常: " + e.getMessage());
        }

    }
}